package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

public class Mhrmabsence extends X_hrm_absence {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1583168766515860160L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmabsence.class);

	public Mhrmabsence(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}
	
	public Mhrmabsence(Properties ctx, int hrm_absence_ID, String trxName) {
		super(ctx, hrm_absence_ID, trxName);
	}
	
	@Override
	public boolean delete(boolean force) {
		
		// Entry is added by the order
		if(gethrm_order_ID() != 0)
			throw new AdempiereException(Msg.translate(getCtx(), "DeleteErrorAbsence"));
		
		return super.delete(force);
	}
	
	@Override
	protected boolean beforeSave(boolean newRecord) {
		// Entry is added by the order

	/**	if(gethrm_order().gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Review)
		if(gethrm_order_ID() != 0) 
			if(gethrm_order().gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Review) 
				&& gethrm_order_ID() != 0 
				&& !newRecord)
			throw new AdempiereException(Msg.translate(getCtx(), "AccessCannotUpdate"));
	*/	
		return true;
	}
	
	public static Mhrmabsence[] getAbsenceType(int C_BPartner_ID, Timestamp sdt, Timestamp edt, String atype) {
		
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmabsence result = null;
			
		ArrayList<Mhrmabsence> list = new ArrayList<Mhrmabsence>();
		// 
			String sql_ = "SELECT *	FROM HRM_ABSENCE \n" +
							 "WHERE ISACTIVE = 'Y' \n" +
								"AND AD_CLIENT_ID ="  + Env.getAD_Client_ID(Env.getCtx()) + " \n" +
								"AND C_BPARTNER_ID =" + C_BPartner_ID   + " \n" +
								"AND (STARTDATE BETWEEN " + DB.TO_DATE(sdt) + " AND " + DB.TO_DATE(edt) + " \n" +
								"OR ENDDATE BETWEEN " + DB.TO_DATE(sdt) + " AND " + DB.TO_DATE(edt) + ") \n" +
								"AND HRM_ABSENCETYPE ='" + atype + "' \n";
			try
			{
				pstmt = DB.prepareStatement(sql_,null);				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					result = new  Mhrmabsence(Env.getCtx(), rs, null);
					list.add(result);
				}
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			
			return list.toArray(new Mhrmabsence[list.size()]);		
	}
	
	public static Mhrmabsence getAbsenceOrder(int C_BPartner_ID, int order_ID) {
		
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmabsence result = null;
		
		String sql = "SELECT *	FROM HRM_ABSENCE \n" +
						 "WHERE HRM_ORDER_ID = " + order_ID + " \n" +
						 "AND C_BPARTNER_ID = " + C_BPartner_ID;
		try
		{
			pstmt = DB.prepareStatement(sql,null);				
			rs = pstmt.executeQuery();	
			if(rs.next())
				result = new  Mhrmabsence(Env.getCtx(), rs, null);
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}			
		
		return result;	
	}	
	
}
